package sanrenxing.dayEnd.backRepertoryTotal.controller;

import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sanrenxing.dayEnd.backRepertoryTotal.dao.FundAllocationDao;
import sanrenxing.dayEnd.backRepertoryTotal.dao.TransferInstructionDao;
import sanrenxing.dayEnd.backRepertoryTotal.domain.CashAccount;
import sanrenxing.dayEnd.backRepertoryTotal.domain.FundAllocation;
import sanrenxing.dayEnd.backRepertoryTotal.domain.TransferInstruction;
import sanrenxing.dayEnd.backRepertoryTotal.service.TransferInstructionBiz;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.SimpleFormatter;

/**
 * @ClassName:TransferInstructionController
 * @description:孙祥兵  划款指令控制器
 * @date:2021/3/18 14:02
 * @version:1.0
 **/
@RestController
@RequestMapping("/Transfer")
public class TransferInstructionController {
    //调用划款指令业务层
    @Autowired
    private TransferInstructionBiz transferInstructionBiz;

    //调用资金调拨现金账户
    @Autowired
    private FundAllocationDao fundAllocationDao;

    /**
     * 多条件查询
     * @param req 请求对象
     * @param page 分页
     * @param limit 行数
     * @return 返回集合
     * @throws ParseException
     */
    @GetMapping("/tran")
    public Map<String,Object>selectTransferInstruction(HttpServletRequest req,int page,int limit) throws ParseException {
        //定义一个用于多条件查询的对象
        TransferInstruction transferInstruction=new TransferInstruction();
        //接收划款日期
        String tranDate=req.getParameter("tranDate");

        //判断是否加入划款对象中
        if (tranDate!=null && tranDate.trim().length()!=0){
            transferInstruction.setTranDate(tranDate);
        }

        //接收到账日期
        String arrival=req.getParameter("arrival");

        //判断是否加入划款对象中
        if (arrival!=null && arrival.trim().length()!=0){
            transferInstruction.setArrival(arrival);
        }

        //调用数据方法，根据划款对象结构查询字符串
        String condition=transferInstructionBiz.getCondition(transferInstruction);
        //执行多条件分页
        List<TransferInstruction> transferInstructionList=transferInstructionBiz.findAll(limit,page,condition);
        //多条件查询表中总统计记录数
        int totalRows=transferInstructionBiz.count(condition);

        //定义映射集合对象
        Map<String,Object> mapData=new HashMap<>();
        //在映射集合中添加key为code,值为0的状态码
        mapData.put("code",0);
        //在映射集合中添加key为msg,值为响应信息字符串
        mapData.put("msg","");
        //在映射集合中添加key为count,值总行数的数据对
        mapData.put("count",totalRows);
        //在映射集合中添加key为data,值为划款对象集合
        mapData.put("data",transferInstructionList);
        //在映射集合中添加key为CashAccountT,值为通过数据层查询现金账户
        mapData.put("cashAccountT",fundAllocationDao.queryCashAccountT());
        //直接返回对象
        return mapData;
    }

    /**
     * 增加一行数据
     * @param transferInstruction 划款对象
     * @return 0：增加成功，1：增加失败
     */
    @PostMapping("/Transfer")
    public String add(@RequestBody TransferInstruction transferInstruction){
        int rows=transferInstructionBiz.add(transferInstruction);
        //返回结果
        return rows>0?"add success":"add failure";
    }

    /**
     * 删除一行数据
     * @param tranId 划款编号
     * @return 0：删除成功，1：删除失败
     */
    @DeleteMapping("/tran/{tranId}")
    public String deleteById(@PathVariable int tranId){
        int rows=transferInstructionBiz.delete(tranId);
        //返回结果
        return rows>0?"delete success":"delete failure";
    }

    /**
     * 修改一行数据
     * @param transferInstruction 划款对象
     * @return 0：修改成功，1：修改失败
     */
    @PutMapping("/Transfer")
    public String update(@RequestBody TransferInstruction transferInstruction){
        int rows=transferInstructionBiz.update(transferInstruction);
        //返回结果
        return rows>0?"update success":"update failure";
    }

    /**
     * 批量删除
     * @param tranIdStr 定义划款编号字符串
     * @return 0：批量删除成功，1：批量删除失败
     */
    @PutMapping("/batchDel")
    public String batchTransferInstruction(@RequestParam String tranIdStr){
        //对划款编号字符串按','分割
        String[] tranIdStrs=tranIdStr.split(",");
        //定义一个返回值
        String result="delete success";
        //循环删除
        for (String str:tranIdStrs){
            //转换类型
            int tranId=Integer.parseInt(str);
            //调用业务层根据编号删除资金调拨
            int rows=transferInstructionBiz.delete(tranId);
            //判断
            if (rows<0){
                result="batch delete failure";
                break;
            }
        }
        return result;
    }

    /**
     * 查询账户
     * @param tranId 划款编号
     * @param caId 划款账户
     * @return 返回集合
     */
    @PutMapping("/method/selectById/{tranId}/{caId}")
    public Map<String,Object> selectById(@PathVariable String tranId,@PathVariable String caId){
        //定义一个map对象
        Map<String,Object> map=new HashMap<>();
        //调用查询单行的方法
        TransferInstruction transferInstruction = transferInstructionBiz.selectById(tranId);
        //定义调用业务层方法查询账户的信息
        CashAccount cashAccount = transferInstructionBiz.selectCashById(caId);
        map.put("ti",transferInstruction);
        map.put("ac",cashAccount);
        return map;
    }
}
